package com.sywb.chuangyebao.contract;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.InterestIndustryInfo;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.fragment.QAndAListFragment;
import com.sywb.chuangyebao.widget.ColorTrackTabLayout;

import org.bining.footstone.adapter.BasePagerFragmentAdapter;
import org.bining.footstone.db.DbManager;
import org.bining.footstone.db.assit.QueryBuilder;
import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;
import org.bining.footstone.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public interface QAndATypeContract {

    interface View extends IView {

        FragmentManager getUserFragmentManager();

        ColorTrackTabLayout getTabLayout();

        ViewPager getViewpager();

        void initCategorieError();

    }

    class Presenter extends BasePresenter<View> {

        private ColorTrackTabLayout tabLayout;
        private ViewPager viewPager;

//        private List<Channel> mSelectedDatas;

        private List<Fragment> mFragments;
        private FragmentPagerAdapter fragmentPagerAdapter;
        private List<Integer> industryIds;

        @Override
        public void onStart() {
            tabLayout = mView.getTabLayout();
            viewPager = mView.getViewpager();

//            mSelectedDatas = new ArrayList<>();
            mFragments = new ArrayList<>();
            industryIds = new ArrayList<>();

//            String[] titleStr = mContext.getResources().getStringArray(R.array.trade_types);
//            for (int i = 0; i < titleStr.length; i++) {
//                String t = titleStr[i];
//                int code = i + 1;
//                mSelectedDatas.add(new Channel(t, code));
//                OtherFragment fragment = OtherFragment.newInstance(2, code + "");
//                mFragments.add(fragment);
//            }
//            fragmentPagerAdapter = new ChannelPagerFragmentAdapter(mView.getUserFragmentManager(), mFragments, mSelectedDatas);
//            viewPager.setAdapter(fragmentPagerAdapter);
//            viewPager.setOffscreenPageLimit(mSelectedDatas.size());

            tabLayout.setTabPaddingLeftAndRight(ScreenUtils.dp2px(10), ScreenUtils.dp2px(10));
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.post(new Runnable() {
                @Override
                public void run() {
                    //设置最小宽度，使其可以在滑动一部分距离
                    ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
                    slidingTabStrip.setMinimumWidth(slidingTabStrip.getMeasuredWidth());
                }
            });
            //隐藏指示器
            tabLayout.setSelectedTabIndicatorHeight(0);

//            //TODO 只在这个版本需要 下个版本删除
//            DbManager.getInstance().deleteAll(InterestIndustryInfo.class);

            List<InterestIndustryInfo> categories = DbManager.getInstance().query(new QueryBuilder<InterestIndustryInfo>(InterestIndustryInfo.class).appendOrderAscBy("sort"));
            if (categories != null && categories.size() > 0) {
//                Collections.sort(categories, new Comparator<InterestIndustryInfo>() {
//                    @Override
//                    public int compare(InterestIndustryInfo interestIndustryInfo, InterestIndustryInfo t1) {
//                        return interestIndustryInfo.sort - t1.sort;
//                    }
//                });
                initCategorieData(categories);
            } else {
                HttpUtils.listCategory(0, new HttpCallback<List<InterestIndustryInfo>>() {
                    @Override
                    public void onSuccess(List<InterestIndustryInfo> categories) {
                        if (categories != null && categories.size() > 0) {

                            for (int i = 0; i < categories.size(); i++) {
                                InterestIndustryInfo interestIndustryInfo = categories.get(i);
                                interestIndustryInfo.sort = i;
                                switch (interestIndustryInfo.name) {
                                    default:
                                    case "创业交流":
                                        interestIndustryInfo.face = R.drawable.commu_fill;
                                        break;
                                    case "餐饮美食":
                                        interestIndustryInfo.face = R.drawable.food_fill;
                                        break;
                                    case "服装鞋包":
                                        interestIndustryInfo.face = R.drawable.clothing;
                                        break;
                                    case "生活服务":
                                        interestIndustryInfo.face = R.drawable.service_filll;
                                        break;
                                    case "酒水饮料":
                                        interestIndustryInfo.face = R.drawable.drink_fill;
                                        break;
                                    case "家居用品":
                                        interestIndustryInfo.face = R.drawable.furnish_fill;
                                        break;
                                    case "美容保养":
                                        interestIndustryInfo.face = R.drawable.cosmet_fill;
                                        break;
                                    case "汽车服务":
                                        interestIndustryInfo.face = R.drawable.car_fill;
                                        break;
                                    case "建材装饰":
                                        interestIndustryInfo.face = R.drawable.material_fill;
                                        break;
                                    case "礼品饰品":
                                        interestIndustryInfo.face = R.drawable.gift_fill;
                                        break;
                                    case "环保机械":
                                        interestIndustryInfo.face = R.drawable.protection_fill;
                                        break;
                                    case "母婴用品":
                                        interestIndustryInfo.face = R.drawable.baby_fill;
                                        break;
                                    case "教育网络":
                                        interestIndustryInfo.face = R.drawable.education_fill;
                                        break;
                                }

                                InterestIndustryInfo localInfo = DbManager.getInstance().queryById(interestIndustryInfo.categoryId, InterestIndustryInfo.class);
                                if (localInfo != null) {
                                    interestIndustryInfo.isChecked = localInfo.isChecked;
                                } else {
                                    interestIndustryInfo.isChecked = false;
                                }
                            }
                            DbManager.getInstance().deleteAll(InterestIndustryInfo.class);
                            DbManager.getInstance().save(categories);

                            initCategorieData(categories);
                        } else {
                            initCategorieError();
                        }
                    }

                    @Override
                    public void onError(String strMsg) {
                        super.onError(strMsg);
                        initCategorieError();
                    }
                });
            }
        }

        public void initCategorieData(List<InterestIndustryInfo> categories) {
            industryIds.clear();
            for (int i = 0; i < categories.size(); i++) {
                InterestIndustryInfo category = categories.get(i);
                industryIds.add(category.categoryId);
                Fragment fragment = QAndAListFragment.newInstance(category.categoryId);
                mFragments.add(fragment);
            }
            fragmentPagerAdapter = new ChannelPagerFragmentAdapter(mView.getUserFragmentManager(), mFragments, categories);
            viewPager.setAdapter(fragmentPagerAdapter);
            viewPager.setOffscreenPageLimit(categories.size());
        }

        public void initCategorieError() {
            if (mView != null) {
                mView.initCategorieError();
            }
        }

        public void changeCategory(String data) {
            int size = industryIds.size();
            if (size == 0) {
                return;
            }

            int index = industryIds.indexOf(Integer.valueOf(data));

            if (index == viewPager.getCurrentItem()) {
                return;
            }

            if (index > 0 && index < size) {
                viewPager.setCurrentItem(index);
            }
        }
    }

    class ChannelPagerFragmentAdapter extends BasePagerFragmentAdapter {

        private List<InterestIndustryInfo> channels;

        public ChannelPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<InterestIndustryInfo> channels) {
            super(fragmentManager, fragments);
            this.channels = channels;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return channels.get(position).name;
        }
    }

}

